package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.clqteam.dao.insertShDao;
import com.clqteam.util.JDBCutil;

public class insertShDaoImpl implements insertShDao{

	@Override
	public int insertShByAllInfo(String pName, String pType, int pAmount, String pClass, double pCost,double pPrice,String pTime,
			int supId) {
		int m = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO s_product (pName,pType,pAmount,pClass,pCost,pPrice,pTime,supId)");
		sql.append(" VALUES (?,?,?,?,?,?,?,?)");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, pName);
			pst.setString(2, pType);
			pst.setInt(3, pAmount);
			pst.setString(4, pClass);
			pst.setDouble(5, pCost);
			pst.setDouble(6, pPrice);
			pst.setString(7, pTime);
			pst.setInt(8, supId);
			int rst = pst.executeUpdate();
			if (rst==1) {
				m=rst;
			}else {
				m=0;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return m;
	}

	@Override
	public void setdirInfo(String drInfo,String drTime,int eId,String eName) {
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO s_dailyreport");
		sql.append(" (drTime,drInfo,eId,eName) VALUES (?,?,?,?)");
		Connection con  = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, drTime);
			pst.setString(2, drInfo);
			pst.setInt(3, eId);
			pst.setString(4, eName);
			pst.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}

	@Override
	public void setOrder(String oTime,int supId,int oClassId,double oMoney,int eId,int vId,String code) {
		 StringBuffer sql = new StringBuffer();
		 sql.append(" INSERT INTO s_order (oClassId,oMoney,eId,oTime,vId,code)");
		 sql.append(" VALUES (?,?,?,?,?,?)");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, oClassId);
			pst.setDouble(2, oMoney);
			pst.setInt(3, eId);
			pst.setString(4, oTime);
			pst.setInt(5, vId);
			pst.setString(6, code);
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		 
		
	}

	@Override
	public int getOidByCode(String code) {
		int m = 0;
		StringBuffer sql = new StringBuffer();
		 sql.append(" SELECT s_order.oId FROM s_order");
		 sql.append(" WHERE s_order.code = ?");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, code);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) {
				m=rst.getInt("oId");
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return m;
	}

	@Override
	public void setOrderInfo(int oId, int pId, int poAmount, double poPrice) {
		StringBuffer sql = new StringBuffer();
		 sql.append(" INSERT INTO s_orderinfo (s_orderinfo.oId,s_orderinfo.pId,s_orderinfo.poAmount,s_orderinfo.poPrice)");
		 sql.append(" VALUES (?,?,?,?)");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, oId);
			pst.setInt(2, pId);
			pst.setInt(3, poAmount);
			pst.setDouble(4, poPrice);
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void setAccountant(int oId) {
		StringBuffer sql = new StringBuffer();
		 sql.append(" INSERT INTO s_accountant (oId)");
		 sql.append(" VALUES (?)");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, oId);
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public int getPidByAllInfo(String pName, String pType, int pAmount, String pClass, double pCost, double pPrice,
			String pTime, int supId) {
		
		int m = 0;
		StringBuffer sql = new StringBuffer();
		 sql.append(" SELECT s_product.pId FROM s_product");
		 sql.append(" WHERE pName = ?");
		 sql.append(" AND pType =?");
		 sql.append(" AND pAmount=?");
		 sql.append(" AND pClass = ?");
		 sql.append(" AND pCost = ?");
		 sql.append(" AND pPrice =?");
		 sql.append(" AND pTime =?");
		 sql.append(" AND supId =?");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, pName);
			pst.setString(2, pType);
			pst.setInt(3, pAmount);
			pst.setString(4, pClass);
			pst.setDouble(5, pCost);
			pst.setDouble(6, pPrice);
			pst.setString(7, pTime);
			pst.setInt(8, supId);
			ResultSet rst = pst.executeQuery();
			if (rst.next()) {
				m=rst.getInt("pId");
				
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		
		return m;
	}

}
